<template>
  <div>
    <!-- 面包屑的顶部：-->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>管理员管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 分割线 -->
    <el-divider></el-divider>

    <!-- 添加相册的按钮 -->
    <div style="margin-bottom: 20px">
      <el-button type="primary" size="medium"
                 @click="$router.push('/sys-admin/permission/admin/add-new')">添加管理员
      </el-button>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column label="头像" width="60" align="center">
        <template slot-scope="scope">
          <el-avatar :size="30" :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="120" header-align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="100" header-align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="phone" label="手机号码" width="120" align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="email" label="电子邮箱" width="150" header-align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="description" label="简介" header-align="center"
                       :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="是否启用" width="120" align="center">
        <template slot-scope="scope">
          <el-switch
              @change="toggleEnable(scope.row)"
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页控件-->
    <!-- total总共记录条数 -->
    <!-- pageSize显示的页数 -->
    <!-- currentPage当前页数 -->
    <div style="text-align: right; margin: 10px auto;">
      <el-pagination
          @current-change="changePage"
          layout="total, prev, pager, next"
          :total="total"
          :currentPage="currentPage"
          :page-size="pageSize">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      pageSize: 0,
      currentPage: 1,
      dialogFormVisible: false,
      editForm: {
        id: '',
        username: '',
        nickname: '',
        phone: '',
        email: '',
        description: '',
      }
    }
  },
  methods: {
    openEditDialog(admin) {
      let title = '提示';
      let message = '您正在尝试编辑【' + admin.id + '-' + admin.username + '】的管理员，抱歉，此功能尚未实现……';
      this.$alert(message, title, {
        confirmButtonText: '确定'
      });
    },
    //修改管理员的方法
    submitEditForm() {
      let url = 'http://localhost:9181/admin/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      let localJwt = localStorage.getItem('localJwt');

      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改管理员信息成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadAlbumList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAlbumList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    //点击删除按钮调用删除的方法
    openDeleteConfirm(admin) {
      this.$confirm('此操作将永久删除【' + admin.id + '-' + admin.username + '】管理员，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(admin);
      }).catch(() => {
      });
    },
    //删除管理员的方法
    handleDelete(admin) {
      let url = 'http://localhost:9181/admin/delete';
      console.log('url = ' + url);

      let formData = 'id=' + admin.id;
      console.log('formData = ' + formData);

      let localJwt = localStorage.getItem('localJwt');

      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除管理员成功！',
            type: 'success'
          });
          this.loadAdminList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAdminList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    //换页的方法
    changePage(value) {
      this.$router.replace('?page=' + value);
      this.loadAdminList();
    },
    //换页刷新页面的方法
    loadAdminList() {
      let page = this.$router.currentRoute.query.page; //获取当前路由网址的参数
      if (!page) { //判断第一次进入到网址时如果没有参数则设置页数的参数为1
        page = 1;
      }

      let url = 'http://localhost:9181/admin/list?page=' + page;
      console.log('url = ' + url);

      let localJwt = localStorage.getItem('localJwt');

      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    }
  },
  mounted() {
    this.loadAdminList();
  }
}
</script>